package com.xnyzc.lhy.common.entity;


import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 优惠券 接口返回样式
 */
@Data
@ApiModel(value = "返回实体", description = "返回实体")
public class CouponRv<T> implements Serializable {

    /**
     * 错误码
     */
    @ApiModelProperty(value = "错误码，SUCCESS ： 成功 OTHER： 错误", example = "0")
    private int code;

    /**
     * 返回msg
     */
    @ApiModelProperty(value = "状态描述")
    private String msg;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private T data;

    /**
     * 是否成功
     */
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    private CouponRv() {
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> CouponRv wrap(T data) {
        CouponRv rv = new CouponRv();

        rv.setCode(CouponErrorCode.success.getValue());
        rv.setMsg(CouponErrorCode.success.toChString());
        rv.setSuccess(true);
        rv.setData(data);
        return rv;
    }

    /**
     * 获取成功实例
     *
     * @return
     */
    public static <T> CouponRv wrapSuccess() {
        CouponRv rv = new CouponRv();
        rv.setCode(CouponErrorCode.success.getValue());
        rv.setMsg(CouponErrorCode.success.toChString());
        return rv;
    }

    /**
     * 获取成功实例
     *
     * @return
     */
    public static <T> CouponRv wrapSuccess(T data) {
        CouponRv rv = new CouponRv();
        rv.setCode(CouponErrorCode.success.getValue());
        rv.setMsg(CouponErrorCode.success.toChString());
        rv.setData(data);
        return rv;
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> CouponRv error(CouponErrorCode data) {
        CouponRv rv = new CouponRv();

        rv.setCode(data.getValue());
        rv.setMsg(data.toChString());
        return rv;
    }

    /**
     * 获取失败实例
     *
     * @return
     */
    public static <T> CouponRv wrapErrorCode(CouponErrorCode code) {
        CouponRv rv = new CouponRv();
        rv.setCode(code.getValue());
        rv.setMsg(code.getDesc());
        return rv;
    }

    /**
     * 获取实例
     *
     * @param msg
     * @param data
     * @return
     */
    public static <T> CouponRv wrap(CouponErrorCode code, String msg, T data) {
        CouponRv rv = new CouponRv();

        rv.setCode(code.getValue());
        rv.setMsg(msg != null ? msg : code.getDesc());
        rv.setData(data);
        return rv;
    }

    /**
     * 获取实例
     *
     * @param data
     * @return
     */
    public static <T> CouponRv wrap(CouponErrorCode code, T data) {
        CouponRv rv = new CouponRv();

        rv.setCode(code.getValue());
        rv.setMsg(code.getDesc());
        rv.setData(data);
        rv.setSuccess(false);
        return rv;
    }

    public boolean isSuc() {
        return this.getCode() == CouponErrorCode.success.getValue().intValue();
    }

    public String toString() {
        return JSON.toJSONString(this);
    }
}
